{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "//anaconda/lib/python2.7/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n",
      "//anaconda/lib/python2.7/site-packages/sklearn/grid_search.py:43: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. This module will be removed in 0.20.\n",
      "  DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "# IMPORTS\n",
    "import pandas as pd\n",
    "import patsy\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "from sklearn.model_selection import train_test_split\n",
    "# from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import classification_report, confusion_matrix\n",
    "from sklearn.grid_search import GridSearchCV\n",
    "from xgboost import XGBClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age                 0\n",
      "Sex                 0\n",
      "Job                 0\n",
      "Housing             0\n",
      "Saving accounts     0\n",
      "Checking account    0\n",
      "Credit amount       0\n",
      "Duration            0\n",
      "Purpose             0\n",
      "Class               0\n",
      "dtype: int64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:652: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  return safe_is_pandas_categorical_dtype(data.dtype)\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n",
      "//anaconda/lib/python2.7/site-packages/patsy/util.py:679: DeprecationWarning: pandas.core.common.is_categorical_dtype is deprecated. import from the public API: pandas.api.types.is_categorical_dtype instead\n",
      "  if safe_is_pandas_categorical_dtype(dt1):\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv('german_credit_risk.csv', index_col=0)\n",
    "# FILL NA OR MISSING VALUES IN CHECKING ACCOUNT / SAVINGS ACCOUNTS AS NOT APPLICABLE OR NO FACILITY\n",
    "df.fillna('Not Applicable', inplace=True)\n",
    "print df.isnull().sum()\n",
    "\n",
    "# CONVERT SEX TO A BINARY INTEGER VARIABLE\n",
    "df['Sex'] = [1 if x == 'female' else 0 for x in df['Sex']]\n",
    "# CONVERT CLASS TO 0 and 1 INSTEAD OF 1 AND 2\n",
    "df['Class'] = [0 if x == 1 else 1 for x in df['Class']]\n",
    "\n",
    "df.rename(index=str, columns={\"Saving accounts\": \"Saving_accounts\", \n",
    "                              \"Checking account\": \"Checking_account\", \n",
    "                              \"Credit amount\": \"Credit_amount\"}, inplace=True)\n",
    "\n",
    "patsy_formula = \"Class~Age+C(Job)+C(Housing)+C(Saving_accounts)+C(Checking_account)+Credit_amount+Duration+Purpose-1\"\n",
    "y, X = patsy.dmatrices(patsy_formula, data=df, return_type='dataframe')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "ss = StandardScaler()\n",
    "Xs = ss.fit_transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "//anaconda/lib/python2.7/site-packages/sklearn/utils/validation.py:526: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
       "          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n",
       "          penalty='l2', random_state=6, solver='liblinear', tol=0.0001,\n",
       "          verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr = LogisticRegression(random_state=6)\n",
    "lr.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             precision    recall  f1-score   support\n",
      "\n",
      "        0.0       0.77      0.92      0.84       140\n",
      "        1.0       0.66      0.35      0.46        60\n",
      "\n",
      "avg / total       0.73      0.75      0.72       200\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print classification_report(y_test, lr.predict(X_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
